<template>
  <el-affix position="bottom" :offset="80">
    <div class="callback float">
      <i v-show="symbol">{{ num }}</i>
      <img @click="toShopCart" src="@/assets/shopCart.png" />
    </div>
  </el-affix>
</template>

<script>
import { ref, onMounted, watch } from "vue";
import { useRouter } from "vue-router";
import { seachUserCart } from "@/api/api.js";
import { useStore } from "vuex";
import VueEvent from "@/js/mitt.js";
export default {
  name: "SuspendCart",
  setup() {
    const router = useRouter();
    const symbol = ref(false);
    let num = ref("");
    const store = useStore();
   VueEvent.on("isShow", (e) => {
      num.value = ""
   })
    // 登录用户的id
    let show = store.state.userInfo.info != null;
    VueEvent.on("show", (e) => {
      console.log("#$" + e);
      if (show) {
        console.log(show);
        const userId = store.state.userInfo.info.id;
        seachUserCart("get", userId).then((res) => {
          num.value = res.data.length;
        });
      }
    });
    if (show) {
        console.log(show);
        const userId = store.state.userInfo.info.id;
        seachUserCart("get", userId).then((res) => {
          num.value = res.data.length;
        });
      }

    // 页面加载获取该用户购物车数据
    onMounted(() => {});
    watch(
      num,
      (nv, ov) => {
        console.log("jj");
        if (nv > 0) {
          symbol.value = true;
        } else {
          symbol.value = false;
        }
      },
      { deeep: true, immediate: true }
    );
    const toShopCart = () => {
      router.push("/ShoppingCart");
    };
    return {
      toShopCart,
      num,
      symbol,
    };
  },
};
</script>

<style lang="scss" scoped>
.callback img {
  display: block;

  width: 50px;

  height: 50px;

  box-shadow: 0 0 10px rgb(133, 129, 129);

  border-radius: 50%;

  background: #fff;
}
.float {
  position: fixed;

  right: 70px;

  touch-action: none;

  text-align: center;

  width: 50px;

  height: 50px;

  border-radius: 24px;

  line-height: 48px;

  color: white;
}
i {
  display: block;
  background: #f00;
  color: #fff;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  top: 0px;
  right: 0px;
  text-align: center;
  line-height: 20px;
  position: absolute;
}
</style>